package com.lyh.hkcloud.util.http;

import android.content.Context;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.lyh.hkcloud.util.base.LogUtil;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.net.ssl.HttpsURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class HttptRequest {

    private static AsyncHttpClient client = new AsyncHttpClient(true,0,0);

    /**
     * GET请求
     */
    public static void get(String url, RequestParams params, JsonHttpResponseHandler responseHandler) {
        if (params == null) {
            params = new RequestParams();
        }

        try {
            LogUtil.i("REQUEST_URL", "GET:" + url + "&" + params);
            client.setTimeout(120 * 1000);            //设置超时时间10s
            CookieUtils.setCookie(client);         //保存cookie
            client.get(url, params, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
            responseHandler.onFailure(-1, new Header[]{}, new Throwable(e.getMessage()), new JSONObject());
        }

    }

    public static void post(Context context, String url, Map<String, String> headMap, JSONObject jsonObject, JsonHttpResponseHandler responseHandler) {

        client.setTimeout(120 * 1000);             //设置超时时间10s
        CookieUtils.setCookie(client);          //保存cookie
        List<Header> headerList = new ArrayList<>();
        for (Map.Entry<String, String> entry : headMap.entrySet()) {
            headerList.add(new BasicHeader(entry.getKey(), entry.getValue()));
        }
        Header[] headers = new Header[headerList.size()];
        headers = headerList.toArray(headers);
        try {
            StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");

            client.post(context, url, headers, entity, "application/json;charset=UTF-8", responseHandler);

            LogUtil.i("REQUEST_URL", "POST:" + url + "---------" + jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
            responseHandler.onFailure(-1, new Header[]{}, new Throwable(e.getMessage()), new JSONObject());
        }
    }

    public static void post(Context context, String url, Map<String, String> headMap, JSONArray jsonArray, JsonHttpResponseHandler responseHandler) {

        client.setTimeout(120 * 1000);             //设置超时时间10s
        CookieUtils.setCookie(client);          //保存cookie
        List<Header> headerList = new ArrayList<>();
        for (Map.Entry<String, String> entry : headMap.entrySet()) {
            headerList.add(new BasicHeader(entry.getKey(), entry.getValue()));
        }
        Header[] headers = new Header[headerList.size()];
        headers = headerList.toArray(headers);
        try {
            StringEntity entity = new StringEntity(jsonArray.toString(), "utf-8");

            client.post(context, url, headers, entity, "application/json;charset=UTF-8", responseHandler);
            LogUtil.i("REQUEST_URL", "POST:" + url + "---------" + jsonArray.toString());
        } catch (Exception e) {
            e.printStackTrace();
            responseHandler.onFailure(-1, new Header[]{}, new Throwable(e.getMessage()), new JSONObject());
        }
    }

    public static void post(Context context, String url, JSONObject jsonObject, JsonHttpResponseHandler responseHandler) {
        client.setTimeout(120 * 1000);             //设置超时时间10s
        CookieUtils.setCookie(client);          //保存cookie
        try {
            StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");
            client.post(context, url, entity, "application/json;charset=UTF-8", responseHandler);
            LogUtil.i("REQUEST_URL", "POST:" + url + "---------" + jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
            responseHandler.onFailure(-1, new Header[]{}, new Throwable(e.getMessage()), new JSONObject());
        }
    }

    /**
     * POST请求
     */
    public static void post(String url, RequestParams params, JsonHttpResponseHandler responseHandler) {
        client.setTimeout(120 * 1000);             //设置超时时间10s
        CookieUtils.setCookie(client);          //保存cookie
        if (params == null) {
            params = new RequestParams();
        }
        try {
            client.post(url, params, responseHandler);
            LogUtil.i("REQUEST_URL", "POST:" + url + "&" + params);
        } catch (Exception e) {
            e.printStackTrace();
            responseHandler.onFailure(-1, new Header[]{}, new Throwable(e.getMessage()), new JSONObject());
        }
    }


    public static void post(String url, RequestParams params, TextHttpResponseHandler responseHandler) {
        client.setTimeout(120 * 1000);             //设置超时时间10s
        CookieUtils.setCookie(client);          //保存cookie
        if (params == null) {
            params = new RequestParams();
        }
        try {
            client.post(url, params, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
            responseHandler.onFailure(-1, new Header[]{}, "", new Throwable(e.getMessage()));
        }
    }

}
